<?php
/*
 * @Author: your name
 * @Date: 2021-09-21 16:46:35
 * @LastEditTime: 2021-12-31 15:20:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Http/Controllers/AdminController/Login.php
 */

namespace App\Http\Controllers\admin_controller;

use Illuminate\Support\Facades\Request;
use App\Models\admin_model\Area as AreaDb;

class Area extends BaseController
{
    /**
     * @description: 登录接口
     */
    public function areaAdd()
    {
      return  redis_mysql_add('area');
    }

    public function areaEdit()
    {
        return  redis_mysql_edit('area');
    }


    public function areaSelOne()
    {
        return  redis_mysql_selone('area');
    }

    public function areaList()
    {

        $limitStr = '';
        if (Request::has('limit') && Request::has('page')) {
            $limitStr = limit_joint(Request::input('page'), Request::input('limit'));
        }
        $wherePidStr = '';
        $whereSonStr = '';
        $areaPidList = [];
        $areaPidCount[0]['count'] = 0;
        if (Request::has('keyWord')) {
            $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
            if (!empty($keyWord)) {
                if (isset($keyWord['areaPidName'])) {
                    $wherePidStr = ' AND `name` LIKE  "%' . $keyWord['areaPidName'] . '%" ';
                }
                if (isset($keyWord['areaSonName'])) {
                    $whereSonStr = ' AND `name` LIKE  "%' . $keyWord['areaSonName'] . '%" ';
                }
            }
        }

        if ($whereSonStr) {
            $areaListSon =  AreaDb::areaListSonZero($whereSonStr);
            $pidInStr = '';
            $pidKeyArray = [];
            $array       =   $this->keyArray($areaListSon,true);
            $pidInStr    =  $array['strId'];
            $pidKeyArray =  $array['keyArray'];
            if( $pidInStr){
                $areaPidList  =   AreaDb::areaListInId( $pidInStr,$limitStr,$wherePidStr);
                $areaPidCount =   AreaDb::areaListInCount($pidInStr,$wherePidStr);
                for($i=0;$i<count($areaPidList);$i++){
                    if( isset( $pidKeyArray[$areaPidList[$i]['id']] ) ){
                        $areaPidList[$i]['children'] = $pidKeyArray[$areaPidList[$i]['id']];
                    }
                }
            }

        //    return $this->create(200, '查询成功', ['list' => $areaPidList, 'total' => $areaPidCount[0]['count']]);

        } else {
            $areaPidList  =   AreaDb::areaList(0, $limitStr, $wherePidStr);
            $areaPidCount =   AreaDb::areaCount(0,$wherePidStr);
            if ($areaPidList) {
                $pidSonStr = "";
                for ($i = 0; $i < count($areaPidList); $i++) {
                    $pidSonStr .= $areaPidList[$i]['id'] . ',';
                }
                $pidSonStr = substr($pidSonStr, 0, -1);
                $areaListSon =  AreaDb::areaListSon($pidSonStr);
                if ($areaListSon) {
                    $keyArray =  $this->keyArray($areaListSon);
                    for ($i = 0; $i < count($areaPidList); $i++) {
                        if (isset($keyArray[$areaPidList[$i]['id']])) {
                            $areaPidList[$i]['children'] = $keyArray[$areaPidList[$i]['id']];
                        }
                    }
                }
            }
        }

        return $this->create(200, '查询成功', ['list' => $areaPidList, 'total' => $areaPidCount[0]['count']]);

    }

    private function keyArray($areaListSon,$strIf = false){
        $keyArray = [];
        $strId = '';
        for ($j = 0; $j < count($areaListSon); $j++) {
            if (!isset($keyArray[$areaListSon[$j]['pid']])) {
                $keyArray[$areaListSon[$j]['pid']] = [];
                if($strIf){
                    $strId .=  $areaListSon[$j]['pid'].',';
                } 
            }
            $keyArray[$areaListSon[$j]['pid']][] = $areaListSon[$j];
        }
        if(!$strIf){
            return  $keyArray;
        }else{
            return   ['keyArray' => $keyArray ,'strId'=>substr( $strId,0,-1)];
        }
    }


    public function areaSelList(){
       $areaListSelList =   AreaDb::areaListSelList();
       $list = [];
       $keyArray = [];
       for($i=0;$i<count( $areaListSelList );$i++){
            if($areaListSelList[$i]['pid'] == 0){
                $list[] = $areaListSelList[$i];
            }else{
                if(!isset($keyArray[$areaListSelList[$i]['pid']])){
                    $keyArray[$areaListSelList[$i]['pid']] = [];
                }
                $keyArray[$areaListSelList[$i]['pid']][] = $areaListSelList[$i];
            }
       }
       for($i=0;$i<count( $list );$i++){
           if(isset($keyArray[ $list[$i]['id'] ] )){
             $list[$i]['child'] = $keyArray[ $list[$i]['id'] ];
           }  
       }
      return $this->create(200,'查询成功',$list);
    }



}
